import 'package:flutter/material.dart';

class Olympic03 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: null,
        // AppBar(
        //   title: Text("2020东京奥运会奖牌榜"),
        //   leading: Icon(Icons.menu),
        //   actions: [Icon(Icons.settings)],
        //   elevation: 0.0,
        //   centerTitle: true,
        // ),
        body: UserList());
  }
}

class User {
  String name;
  int gold;
  int silver;
  int bronze;
  int total;
  bool selected;

  User(this.name, this.gold, this.silver, this.bronze, this.total,
      {this.selected = false});
}

class UserList extends StatefulWidget {
  UserList({Key? key}) : super(key: key);

  @override
  _UserListState createState() => _UserListState();
}

class _UserListState extends State<UserList> {
  List<User> data = [
    User('USA', 39, 41, 33, 113),
    User('CHN', 38, 32, 18, 88, selected: true),
    User('JPN', 27, 14, 17, 58),
    User('GBR', 22, 21, 22, 65),
    User('ROC', 20, 28, 23, 71),
    User('AUS', 17, 7, 22, 46),
    User('NED', 10, 12, 14, 36),
    User('FRA', 10, 12, 11, 33),
    User('GER', 10, 11, 16, 37),
    User('ITA', 10, 10, 20, 40),
  ];

  var _sortAscending = false;
  var _sortColumnIndex = 1;

  List<DataRow> _getUserRows() {
    List<DataRow> dataRows = [];
    for (int i = 0; i < data.length; i++) {
      dataRows.add(DataRow(
          selected: data[i].selected,
          onSelectChanged: (selected) {
            setState(() {
              data[i].selected = selected!;
            });
          },
          cells: [
            DataCell(Image.network(
                "http://p1.img.cctvpic.com/sports/data/olympic/teamImg/" +
                    '${data[i].name}' +
                    ".png")),
            DataCell(Text('${data[i].gold}')),
            DataCell(Text('${data[i].silver}')),
            DataCell(Text('${data[i].bronze}')),
            DataCell(Text('${data[i].total}')),
            // DataCell(Text('男')),
            // DataCell(Text('---')),
          ]));
    }

    return dataRows;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: SingleChildScrollView(
          // scrollDirection: Axis.horizontal,
          scrollDirection: Axis.vertical,
          child: DataTable(
            sortColumnIndex: _sortColumnIndex,
            sortAscending: _sortAscending,
            dataRowHeight: 100,
            horizontalMargin: 20,
            columnSpacing: 30,
            columns: [
              DataColumn(label: Text('国家')),
              DataColumn(
                  label: Text('金牌'),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _sortAscending = asscending;
                      _sortColumnIndex = 1;
                      if (asscending) {
                        data.sort((a, b) => a.gold.compareTo(b.gold));
                      } else {
                        data.sort((a, b) => b.gold.compareTo(a.gold));
                      }
                    });
                  }),
              DataColumn(
                  label: Text('银牌'),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _sortAscending = asscending;
                      _sortColumnIndex = 2;
                      if (asscending) {
                        data.sort((a, b) => a.silver.compareTo(b.silver));
                      } else {
                        data.sort((a, b) => b.silver.compareTo(a.silver));
                      }
                    });
                  }),
              DataColumn(
                  label: Text('铜牌'),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _sortAscending = asscending;
                      _sortColumnIndex = 3;
                      if (asscending) {
                        data.sort((a, b) => a.bronze.compareTo(b.bronze));
                      } else {
                        data.sort((a, b) => b.bronze.compareTo(a.bronze));
                      }
                    });
                  }),
              DataColumn(
                  label: Text('总数'),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _sortAscending = asscending;
                      _sortColumnIndex = 4;
                      if (asscending) {
                        data.sort((a, b) => a.total.compareTo(b.total));
                      } else {
                        data.sort((a, b) => b.total.compareTo(a.total));
                      }
                    });
                  }),
            ],
            rows: _getUserRows(),
            // [
            //   DataRow(
            //     cells: [
            //       DataCell(Text('张三')),
            //       DataCell(Text('18')),
            //       DataCell(Text('男')),
            //       DataCell(Text('一个男人')),
            //     ]
            //   ),
            //   DataRow(
            //     cells: [
            //       DataCell(Text('张三')),
            //       DataCell(Text('18')),
            //       DataCell(Text('男')),
            //       DataCell(Text('一个男人')),
            //     ]
            //   ),
            //   DataRow(
            //     cells: [
            //       DataCell(Text('张三')),
            //       DataCell(Text('18')),
            //       DataCell(Text('男')),
            //       DataCell(Text('一个男人')),
            //     ]
            //   )
            // ]
          )),
    );
  }
}
